/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

/**
 *
 * @author Caio Feitoza
 */
public class ValidacaoCPF {
    
    public static boolean validaCPF(long num){
        String cpf;
        cpf = String.valueOf(num);
        if(cpf == null || cpf.length()!=11 || padraoCPF(cpf)){
            return false;
        }
        return calcVerificador(cpf.substring(0, 9)).equals(cpf.substring(9, 11));
    }
    
    public static boolean padraoCPF(String cpf){
        if(cpf.equals("11111111111") || cpf.equals("22222222222")
        || cpf.equals("33333333333")
        || cpf.equals("44444444444")
        || cpf.equals("55555555555")
        || cpf.equals("66666666666")
        || cpf.equals("77777777777")
        || cpf.equals("88888888888")
        || cpf.equals("99999999999")){
            
            return true;
        }
        return false;
    }
    
    public static String calcVerificador(String cpf){
        Integer primDig, segDig;
        int soma = 0, peso = 10;
        for(int i = 0;i<cpf.length();i++){
            soma += Integer.parseInt(cpf.substring(i, i + 1)) * peso--;
        }
        if(soma%11==0 || soma%11==1){
            primDig = new Integer(0);
        }
        else{
            primDig = new Integer(11-(soma%11));
        }
        soma=0;
        peso=11;
        for(int i = 0;i<cpf.length();i++){
            soma += Integer.parseInt(cpf.substring(i, i + 1)) * peso--;
        }
        soma+= primDig.intValue() * 2;
        if(soma%11==0 || soma%11==1){
            segDig = new Integer(0);
        }
        else{
            segDig = new Integer(11-(soma%11));
        }
        return primDig.toString() + segDig.toString();
    }
}
